package Tra

import org.apache.spark.sql.SparkSession

import java.util.Properties

object t2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().getOrCreate()

    val url = "jdbc:clickhouse://bigdata1:8123/hhd"
    val prop = new Properties()
    prop.put("user","default")
    prop.put("password","123456")
    prop.put("driver","ru.yandex.clickhouse.ClickHouseDriver")

    spark.sql(
      """
        |from hhd.tra
        |   select
        |       areaId area_id,
        |       sum(speed)/count(car) avg,
        |       date_format(actionId,'yyyy-MM-dd') actionTime
        |   group by areaId,date_format(actionId,'yyyy-MM-dd')
        |""".stripMargin)
      .write.mode("append")
      .jdbc(url,"avgSpeed",prop)
  }
}
